package com.mall.user.services;

import com.mall.user.IVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: enhui
 **/
@Service
public class VerifyServiceImpl implements IVerifyService {
    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Autowired
    MemberMapper memberMapper;

    @Override
    public UserVerifyResponse verify(UserVerifyRequest request) {
        UserVerifyResponse response = new UserVerifyResponse();
        try {
            //先查询该用户和uuid，只有一个且未激活
//            validUuidAndUsername(request);
            Example example = new Example(UserVerify.class);
            example.createCriteria().andEqualTo("uuid", request.getUuid()).
                    andEqualTo("username", request.getUserName());
            List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
            if (userVerifies.size() != 1) {
                //注册激活时先查询，失败
                response.setCode(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getCode());
                response.setMsg(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getMessage());
                return response;
            }

            //成功，修改验证状态为Y
            UserVerify userVerify = new UserVerify();
            userVerify.setIsVerify("Y");
            int verifyCount = userVerifyMapper.updateByExampleSelective(userVerify, example);
            //修改用户表验证状态为Y
            Member member = new Member();
            member.setIsVerified("Y");
            member.setUpdated(new Date());
            Example memberExample = new Example(Member.class);
            memberExample.createCriteria().andEqualTo("username", request.getUserName());
            int memberCount = memberMapper.updateByExampleSelective(member, memberExample);
            if (verifyCount != 1 || memberCount != 1) {
                response.setCode(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getCode());
                response.setMsg(SysRetCodeConstants.USER_INFOR_UPDATE_FAIL.getMessage());
                return response;
            }
            response.setCode(SysRetCodeConstants.SUCCESS.getCode());
            response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(SysRetCodeConstants.DB_EXCEPTION.getCode());
            response.setMsg(SysRetCodeConstants.DB_EXCEPTION.getMessage());
        }
        return response;
    }

//    private void validUuidAndUsername(UserVerifyRequest request) {
//        Example example = new Example(UserVerify.class);
//        example.createCriteria().andEqualTo("uuid", request.getUuid()).
//                andEqualTo("username", request.getUserName());
//        List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
//        if (userVerifies.size() != 1) {
//            //注册激活时先查询，失败
//            new ValidateException(SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getCode(),
//                    SysRetCodeConstants.USERVERIFY_INFOR_INVALID.getMessage());
//        }
//    }
}
